package com.com.yunzong.rainbowbridge.network;

import android.util.Log;

import com.yz.jdd.base.MyApplication;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PreferencesUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;

public class LogInterceptor  implements Interceptor {
    private static boolean isText(MediaType mediaType) {
        if (mediaType.type() != null && mediaType.type().equals("text")) {
            return true;
        }
        if (mediaType.subtype() != null) {
            if (mediaType.subtype().equals("json") ||
                    mediaType.subtype().equals("xml") ||
                    mediaType.subtype().equals("html") ||
                    mediaType.subtype().equals("webviewhtml")
                    )
                return true;
        }
        return false;
    }
    private static String bodyToString(final Request request) {
        try {
            final Request copy = request.newBuilder().build();
            final Buffer buffer = new Buffer();
            copy.body().writeTo(buffer);
            return buffer.readUtf8();
        } catch (final IOException e) {
            return "did not work";
        }
    }
    @Override
    public Response intercept(Chain chain) throws IOException {

        String sessionId = PreferencesUtils.getString(MyApplication.application, BaseConfigs
                .PREF_SESSIONID, "");

        Request request = chain.request()
                .newBuilder()
                .addHeader("sessionId", sessionId)

                .build();

            long t1 = System.nanoTime();
            String requestBody = request.method().compareToIgnoreCase("post") == 0 ?
                    bodyToString(request) : "";
            String requestLog = String.format("url: %s on %s %nheaders:%n%s %nbody:%n%s",
                    request.url(), chain.connection(), request.headers(), requestBody);
            LogUtils.d("=== request" + "\n" + requestLog);
            Response response = chain.proceed(request);
            long t2 = System.nanoTime();
            String responseBody;
            boolean rebuildBody = false;
            MediaType mediaType = response.body().contentType();
            if (mediaType != null && !isText(mediaType)) {
                responseBody = "Respose Content-Type: " + mediaType.toString();
                rebuildBody = false;
            } else {
                responseBody = response.body().string();
                rebuildBody = true;
            }

            String responseLog = String.format("url: %s in %.1fms  protocol:%s %nheaders:%n%s" +
                            " " + "%nbody:%n%s", response.request().url(), (t2 - t1) / 1e6d,
                    response.protocol().name(), response.headers(), responseBody);
            LogUtils.d("=== response" + "\n" + responseLog);

            if (rebuildBody) {
                return response.newBuilder()
                        .body(ResponseBody.create(response.body().contentType(), responseBody))
                        .build();
            } else {
                return response;
            }

    }
}

